<template>
  <div class="app-container" v-loading.fullscreen.lock="fullLoading">
    <el-row>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-plus" size="mini" @click="subVClick">提交申请</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="auditDetailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生姓名" align="center" prop="stuName" />
      <el-table-column label="项目大类" align="center" prop="projectName" />
      <el-table-column label="项目子类" align="center" prop="ruleName" />
      <el-table-column label="分数" align="center" prop="operateScore">
        <template slot-scope="scope">
          {{ scope.row.operateScore > 0 ? "+" + scope.row.operateScore.toString() : scope.row.operateScore }}
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="submitTime" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audius_detail_type" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-folder" @click="processVClick(scope.row)">查看详情</el-button>
          <el-button v-if="scope.row.auditStatus == '10'" size="mini" type="text" icon="el-icon-folder-add"
            @click="reSubVClick(scope.row)">重新提交</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="description" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['comprehensive:auditDetails:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['comprehensive:auditDetails:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改审核明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="提交人id" prop="submitterId">
          <el-input v-model="form.submitterId" placeholder="请输入提交人id" />
        </el-form-item>
        <el-form-item label="提交人" prop="submitterName">
          <el-input v-model="form.submitterName" placeholder="请输入提交人" />
        </el-form-item>
        <el-form-item label="审核人id" prop="reviewedById">
          <el-input v-model="form.reviewedById" placeholder="请输入审核人id" />
        </el-form-item>
        <el-form-item label="项目id" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目id" />
        </el-form-item>
        <el-form-item label="项目类型id" prop="projectTypeId">
          <el-input v-model="form.projectTypeId" placeholder="请输入项目类型id" />
        </el-form-item>
        <el-form-item label="审核状态" prop="statusCode">
          <el-select v-model="form.statusCode" placeholder="请选择审核状态">
            <el-option v-for="dict in dict.type.audius_detail_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 思想品德提交申请 -->
    <el-dialog title="思想品德加分" :visible.sync="iamV" width="600px" append-to-body>
      <el-form ref="iamForm" :model="iamForm" label-width="100px">

        <el-form-item label="学年" prop="stuYearId">
          <el-select style="width: 400px;" v-model="iamForm.stuYearId" placeholder="请选择学年">
            <el-option v-for="(v, i) in stu_year_list" :key="v.id" :value="v.id" :label="v.stuYearName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="加分项" prop="childType">
          <el-select style="width: 400px;" v-model="iamChildType" placeholder="请选择加分项">
            <el-option v-for="(v, i) in child_list" :key="v.ruleId" :value="v.ruleId" :label="v.ruleName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分值" prop="description">
          <el-input style="width: 400px;" v-model="iam_add_record" type="number" disabled placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="佐证材料" prop="material">
          <image-upload v-model="iamForm.material" />
        </el-form-item>

        <el-form-item label="备注" prop="description">
          <el-input v-model="iamForm.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitIamForm">确 定</el-button>
        <el-button @click="cancelIam" fit="contain">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog style="margin-top: 0;" title="查看详情" :visible.sync="processV" append-to-body>

      <el-form label-width="100px">
        <el-form-item label="未通过原因" v-if="detailInfo.auditStatus != '6' && detailInfo.auditStatus != '1'">
          <el-input type="textarea" v-model="detailInfo.remarks" readonly />
        </el-form-item>
        <el-form-item label="学院" prop="projectName">
          <el-input v-model="detailInfo.deptName" readonly />
        </el-form-item>
        <el-form-item label="专业" prop="projectName">
          <el-input v-model="detailInfo.majorName" readonly />
        </el-form-item>
        <el-form-item label="班级" prop="projectName">
          <el-input v-model="detailInfo.className" readonly />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="detailInfo.ruleName" readonly />
        </el-form-item>
        <el-form-item label="分数" prop="projectName">
          <el-input v-model="detailInfo.operateScore" readonly />
        </el-form-item>
        <el-form-item label="加分学生" prop="submitterName">
          <el-input v-model="detailInfo.stuName" readonly />
        </el-form-item>
        <el-form-item label="描述" prop="submitterName">
          <el-input v-model="detailInfo.description" type="textarea" readonly />
        </el-form-item>
      </el-form>
      <el-carousel indicator-position="outside">
        <el-carousel-item v-for="(v, i) in material_list" :key="i" style="text-align: center;">
          <image-preview :src="v" :width="400" :height="300" />
          
          <!-- <el-image :src="getImg(v)" style="height: 100%;"></el-image> -->
        </el-carousel-item>
      </el-carousel>


      <div slot="footer" class="dialog-footer">
      </div>
    </el-dialog>


    <el-dialog title="重新提交" :visible.sync="reSubV" width="600px" append-to-body>
      <el-form ref="reSubForm" :model="reSubForm" label-width="100px">

        <el-form-item label="学年" prop="stuYearId">
          <el-select style="width: 400px;" v-model="reSubForm.stuYearId" placeholder="请选择学年">
            <el-option v-for="(v, i) in stu_year_list" :key="v.id" :value="v.id" :label="v.stuYearName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="加分项" prop="childType">
          <el-select style="width: 400px;" v-model="reSubFormiamChildType" placeholder="请选择加分项">
            <el-option v-for="(v, i) in child_list" :key="v.ruleId" :value="v.ruleId" :label="v.ruleName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分值" prop="description">
          <el-input style="width: 400px;" v-model="reSubForm.iam_add_record" type="number" disabled placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="佐证材料" prop="material">
          <image-upload v-model="reSubForm.material" />
        </el-form-item>

        <el-form-item label="备注" prop="description">
          <el-input v-model="reSubForm.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="resubmitIamForm">确 定</el-button>
        <el-button @click="cancelIam" fit="contain">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>
  
<script>
import { listOwnApply, getAuditDetails, delAuditDetails, addAuditDetails, updateAuditDetails, mySubmit, getDetailInfo, reSub } from "@/api/stuCQS/process-center/auditDetails";
import { listEnableYear as listYear } from "@/api/stuCQS/basedata/year";
import { listIamChild } from "@/api/stuCQS/basedata/cphRules";
import { stuAdd } from "@/api/stuCQS/info-fill/iam";
import { isEmpty } from "@/api/helpFunc";


export default {
  name: "AuditDetails",
  dicts: ['audius_detail_type'],
  data() {
    return {
      fullLoading: false,
      reSubV: false,
      reSubForm: {},
      reSubFormiamChildType: null,

      processV: false,
      processForm: {},
      processStatus: 10,

      iamForm: {},
      iamV: false,
      stu_year_list: [],
      child_list: [],
      iam_add_record: 0,
      iamChildType: null,
      detailInfo: {
      },
      material_list: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审核明细表格数据
      auditDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        submitterId: null,
        submitterName: null,
        reviewedById: null,
        projectId: null,
        projectTypeId: null,
        statusCode: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getStuYear();
    this.listIamChild();
  },
  watch: {
    "iamChildType"(newVal, oldVal) {
      this.child_list.map((v) => {
        if (v.ruleId == newVal) {
          this.iam_add_record = v.maxScore
        }
      });
    },
    "reSubFormiamChildType"(newVal, oldVal) {
      this.child_list.map((v) => {
        if (v.ruleId == newVal) {
          this.reSubForm.iam_add_record = v.maxScore
        }
      });
    },
  },
  methods: {
    resubmitIamForm() {
      let temp = { ...this.reSubForm };
      let sdata = {
        iamId: temp.id,
        description: temp.description,
        material: temp.material,
        ruleId: this.reSubFormiamChildType,
        stuYearId: temp.stuYearId
      }

      if (isEmpty(sdata.material)) {
        this.$message.info('请上传大小不超过0.5mb的佐证材料');
        return;
      }
      if (isEmpty(this.reSubFormiamChildType)) {
        this.$message.info('请选择加分项');
        return;
      }
      if (isEmpty(sdata.description)) {
        this.$message.info("请填写备注");
        return;
      }
      this.fullLoading = true;
      reSub(sdata).then(res => {
        this.fullLoading = false;
        if (res.code == 200) {
          this.$modal.msgSuccess("操作成功");
          this.getList();
          this.reSubV = false;
        }

      });
    },
    reSubVClick(v) {
      console.log(v);
      let temp = { ...v };
      this.reSubForm = { ...temp };
      this.reSubFormiamChildType = temp.ruleId;
      this.reSubForm.iam_add_record = temp.operateScore;
      this.reSubV = true;
    },
    //获取图片
    getImg(src) {
      return process.env.VUE_APP_BASE_API + src
    },
    async processVClick(val) {
      console.log(val);
      this.detailInfo = { ...val };
      if (!isEmpty(this.detailInfo.material)) {
        this.material_list = this.detailInfo.material.split(",");
      }

      this.processStatus = 10;
      this.processForm = val;
      this.processV = true;
    },
    /** 提交思想品德 */
    submitIamForm() {
      if (isEmpty(this.iamForm.material)) {
        this.$message.info('请上传大小不超过0.5mb的佐证材料');
        return;
      }
      if (isEmpty(this.iamChildType)) {
        this.$message.info('请选择加分项');
        return;
      }
      if (isEmpty(this.iamForm.description)) {
        this.$message.info("请填写备注");
        return;
      }
      this.$refs["iamForm"].validate(valid => {
        if (valid) {
          let sdata = {
            stuYearId: this.iamForm.stuYearId,
            material: this.iamForm.material,
            description: this.iamForm.description,
            ruleId: this.iamChildType,
          }
          this.fullLoading = true;
          stuAdd(sdata).then(res => {
            this.fullLoading = false;
            if (res.code == 200) {
              this.$modal.msgSuccess("新增成功");
              this.getList();
              this.iamV = false;
            }
          });
        }

      });
    },
    cancelIam() {
      this.iamV = false;
      this.reset();
    },
    reset() {
      this.resetForm("iamForm");
    },

    //列出思想品德子类
    async listIamChild() {
      this.child_list = [];
      let res = await listIamChild();
      let temp = [...res.data];
      temp.map(x => {
        if (x.maxScore >= 0) {
          this.child_list.push(x);
        }
      })
    },
    //列出学年信息
    async getStuYear() {
      let res = await listYear();
      this.stu_year_list = [...res.rows];
      this.iamForm.stuYearId = res.rows[0].id;
    },


    subVClick() {
      //this.listIamChild();
      //this.getStuYear();
      this.iamV = true;

    },

    /** 查询审核明细列表 */
    getList() {
      this.loading = true;
      listOwnApply(this.queryParams).then(response => {
        this.auditDetailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectName: null,
        submitterId: null,
        submitterName: null,
        reviewedById: null,
        projectId: null,
        projectTypeId: null,
        statusCode: null,
        createTime: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审核明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAuditDetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审核明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAuditDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.fullLoading = true;
            addAuditDetails(this.form).then(response => {
              this.fullLoading = false;
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除审核明细编号为"' + ids + '"的数据项？').then(function () {
        return delAuditDetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('comprehensive/auditDetails/export', {
        ...this.queryParams
      }, `auditDetails_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
  